package com.capgemini.fs.presentation.user.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.capgemini.fs.app.ekms.common.Constants;
import com.capgemini.fs.app.ekms.model.Group;
import com.capgemini.fs.app.ekms.service.GroupService;
import com.capgemini.fs.presentation.user.web.form.CreateGroupForm;

public class CreateGroupAction extends DispatchAction {
	private GroupService createGroupService = null;

	public GroupService getCreateGroupService() {
		return createGroupService;
	}

	public void setCreateGroupService(GroupService createGroupService) {
		this.createGroupService = createGroupService;
	}
	public ActionForward listStatus(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// List<Parameter> list =
		// parameterService.findParameterByCategory(Constants.GROUP_STATUS_CATEGORY);
		// request.setAttribute("statusList", list);
		return mapping.findForward("succ");
	}

	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		CreateGroupForm createGroupForm = (CreateGroupForm) form;// TODO
																	// Auto-generated
																	// method
																	// stub
		String[] keyWords = { "Administrator", "Examinee", "Manager", "HR",
				"L & C", "L& C", "L &C", "L&C", "Recruitment" };
		try {
			Group group = serializeGroup(createGroupForm);
			if (null == group.getGroupName().trim()
					|| "".equalsIgnoreCase(group.getGroupName().trim())) {
				request.setAttribute("Failed",
						"Please fill in the mandatory field Group Name!");
				return mapping.findForward("failed");
			}

			List<Group> groupList = createGroupService.findAllGroup();
			for (Group groupl : groupList) {
				if (group.getGroupName().trim().equalsIgnoreCase(
						groupl.getGroupName().trim())) {
					request.setAttribute("Failed", "The " + "\'"
							+ group.getGroupName() + "\'"
							+ " already exist, please fill in again!");
					return mapping.findForward("failed");
				}
			}
			for (int i = 0; i < keyWords.length; i++) {
				if (group.getGroupName().equalsIgnoreCase(keyWords[i])) {
					request.setAttribute("Failed", "The "
							+ group.getGroupName()
							+ " can not be the keyword as " + "\'"
							+ keyWords[i] + "\'" + ",please fill in again!");
					return mapping.findForward("failed");
				}
			}
			createGroupService.createGroup(group);
			request.setAttribute("Success", "Created Successfully!");
			return mapping.findForward("succ");
		} catch (Exception e) {
			request.setAttribute("Failed", e.getMessage());
			return mapping.findForward("failed");
		}
	}

	public Group serializeGroup(CreateGroupForm groupForm) {
		Group group = new Group();
		group.setGroupName(groupForm.getGroupname());
		group.setDescription(groupForm.getDescription());
		if (groupForm.getActive() == 0) {
			group.setStatus(Constants.GROUP_STATUS_ACTIVE);
		} else {
			group.setStatus(Constants.GROUP_STATUS_INACTIVE);
		}
		return group;
	}

	public ActionForward load(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("suc");
	}

}
